<template>
  <div class="mask" v-if="showCart && calculations.total>0" @click="handleCartShowChange"></div>
  <div class='cart'>
    <div class="product" v-if="showCart && calculations.total>0">
      <div class="product_header">
        <div
          class="product_header_all"
          @click="()=>setCartItemsChecked(shopId)"
        >
          <span
            class="product_header_icon iconfont"
            v-html="calculations.allChecked ? '&#xe69b;':'&#xe66c;'"></span>
          全选
        </div>
        <div class="product_header_clear">
          <span @click="()=>cleanCartProduct(shopId)">清空购物车</span>
          </div>
      </div>
      <div v-for="(item,_id) in productList" :key="_id" class="product_item">
        <div
          class="product_item_checked iconfont"
          v-html="item.check ? '&#xe69b;':'&#xe66c;'"
          @click="()=>changeCartItemCheck(shopId,item._id)"></div>
        <img class="product_item_img" :src="item.imgUrl" alt="">
        <div class="product_item_detail">
          <h4 class="product_item_title">{{item.name}}</h4>
          <p class="product_item_price">
            <span class="product_item_yen">&yen;</span>{{item.price}}
            <span class="product_item_origin">&yen;{{item.oldPrice}}</span>
          </p>
        </div>
        <div class="product_number">
          <span
            class="product_number_minus iconfont"
            @click="()=>{changeCartItemInfo(shopId,item._id,item,-1)}">&#xe677;</span>
          <!-- {{cartList?.[shopId]?.[item._id]?.count || 0}} -->
          {{item.count||0}}
          <span
            class="product_number_plus iconfont"
            @click="()=>{changeCartItemInfo(shopId,item._id,item,1)}">&#xe845;</span>
        </div>
      </div>
    </div>
    <div class="check">
      <div class="check_icon">
        <img
          class="check_icon_img"
          src="http://www.dell-lee.com/imgs/vue3/basket.png" alt=""
          @click="handleCartShowChange">
        <div class="check_icon_tag">{{calculations.total}}</div>
      </div>
      <div class="check_info">
        总计：
        <span class="check_info_price">&yen; {{calculations.totalPrice}}</span>
      </div>
      <div class="check_btn" v-show="calculations.total>0">
        <router-link :to="{path:`/OrderConfirm/${shopId}`}">
          去结算
        </router-link>
      </div>
    </div>
  </div>
</template>

<script>
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { ref } from 'vue'
import { useCommonCartEffect } from '@/effects/cartEffects'
// 获取购物车信息逻辑
const useCartEffect = (shopId) => {
  const store = useStore()
  const { productList, calculations, changeCartItemInfo } = useCommonCartEffect(shopId)
  // 计算
  // const calculations = computed(() => {
  //   const productList = cartList[shopId]?.productList
  //   const result = { total: 0, totalPrice: 0, allChecked: true }
  //   if (productList) {
  //     for (const i in productList) {
  //       const product = productList[i]
  //       result.total += product.count
  //       if (product.check) {
  //         result.totalPrice += (product.count * product.price)
  //       }
  //       if (product.count > 0 && !product.check) {
  //         result.allChecked = false
  //       }
  //     }
  //   }
  //   result.totalPrice = result.totalPrice.toFixed(2)
  //   return result
  // })
  // 数量相加
  // const total = computed(() => {
  //   const productList = cartList[shopId]?.productList
  //   let count = 0
  //   if (productList) {
  //     for (const i in productList) {
  //       const product = productList[i]
  //       count += product.count
  //     }
  //   }
  //   return count
  // })
  // 价格总计
  // const totalPrice = computed(() => {
  //   const productList = cartList[shopId]?.productList
  //   let price = 0
  //   if (productList) {
  //     for (const i in productList) {
  //       const product = productList[i]
  //       if (product.check) {
  //         price += (product.count * product.price)
  //       }
  //     }
  //   }
  //   return price.toFixed(2)
  // })

  // const productList = computed(() => {
  //   const productList = cartList[shopId]?.productList || []
  //   return productList
  // })

  // const allChecked = computed(() => {
  //   const productList = cartList[shopId]?.productList
  //   let result = true
  //   if (productList) {
  //     for (const i in productList) {
  //       const product = productList[i]
  //       if (product.count > 0 && !product.check) {
  //         result = false
  //       }
  //     }
  //   }
  //   return result
  // })

  const changeCartItemCheck = (shopId, productId) => {
    store.commit('changeCartItemCheck', { shopId, productId })
  }

  const cleanCartProduct = (shopId) => {
    store.commit('cleanCartProduct', { shopId })
  }

  const setCartItemsChecked = (shopId) => {
    store.commit('setCartItemsChecked', { shopId })
  }
  return {
    productList,
    changeCartItemInfo,
    changeCartItemCheck,
    cleanCartProduct,
    setCartItemsChecked,
    calculations
  }
}
// 展示隐藏购物车逻辑
const toggleCartEffect = () => {
  const showCart = ref(false)
  const handleCartShowChange = () => {
    showCart.value = !showCart.value
  }
  return { showCart, handleCartShowChange }
}
export default {
  name: 'Cart',
  components: {},
  setup () {
    const route = useRoute()
    const shopId = route.params.id
    const { showCart, handleCartShowChange } = toggleCartEffect()
    const {
      productList,
      calculations,
      changeCartItemInfo,
      changeCartItemCheck,
      cleanCartProduct,
      setCartItemsChecked
    } = useCartEffect(shopId)
    return {
      calculations,
      productList,
      shopId,
      showCart,
      changeCartItemInfo,
      changeCartItemCheck,
      cleanCartProduct,
      setCartItemsChecked,
      handleCartShowChange
    }
  }
}
</script>
<style lang='scss' scoped>
@import '@/style/viriables.scss';
@import '@/style/mixins.scss';
.mask{
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 1;
}
.cart{
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 2;
  background: $bg-color;
  // height: .5rem;
}
.product{
  flex: 1;
  overflow-y: scroll;
  background: $bg-color;
  &_header{
    display: flex;
    line-height: .52rem;
    border-bottom: .01rem solid #f1f1f1;
    font-size: .14rem;
    color: #333;
    &_all{
      width: .64rem;
      margin-left: .18rem;
    }
    &_icon{
      color: #0091ff;
      font-size: 0.2rem;
      display: inline-block;
      vertical-align: middle;
    }
    &_clear{
      flex: 1;
      margin-right: .16rem;
      text-align: right;
      &>span{
        display: inline-block;
      }
    }
  }
  &_item{
    position: relative;
    display: flex;
    padding: .12rem 0;
    margin: 0 .16rem;
    border-bottom: .01rem solid $content-bgColor;
    &_checked{
      color: #0091ff;
      font-size: .2rem;
      line-height: .5rem;
      margin-right: .2rem;
    }
    &_detail{
      overflow: hidden;
    }
    &_img{
      width: .46rem;
      height: .46rem;
      margin-right: .16rem;
    }
    &_title{
      margin: 0;
      line-height: .2rem;
      font-size: .14rem;
      color: $content-fontcolor;
      @include ellipse;
    }
    &_price{
      margin: .06rem 0 0 0;
      line-height: .2rem;
      font-size: .14rem;
      color: $highlight-fontColor;
    }
    &_yen{
      font-size: .12rem;
    }
    &_origin{
      margin-left: .06rem;
      line-height: .2rem;
      font-size: .12rem;
      color: $light-fontColor;
      text-decoration: line-through;
    }
    .product_number{
      position:absolute;
      right: 0;
      bottom: .3rem;
      &_minus{
        position: relative;
        top: .02rem;
        color: $medium-fontColor;
        margin-right: .05rem;
      }
      &_plus{
        position: relative;
        top: .02rem;
        color: #0091ff;
        margin-left: .05rem;
      }
    }
  }
}
.check{
  display: flex;
  height: .5rem;
  line-height: .5rem;
  border-top: $content-bgColor;
  &_icon{
    width: .84rem;
    position: relative;
    &_img{
      display: block;
      width: .28rem;
      height: .26rem;
      margin: .12rem auto;
    }
    &_tag{
      position: absolute;
      left: .47rem;
      top: .04rem;
      padding: 0 .04rem;
      min-width: .2rem;
      height: .2rem;
      line-height: .2rem;
      background-color: $highlight-fontColor;
      border-radius: .1rem;
      font-size: .12rem;
      text-align: center;
      color: $bg-color;
      transform: scale(.5);
      transform-origin: left center;
    }
  }
  &_info{
    flex: 1;
    color:$content-fontcolor;
    font-size: .12rem;
    &_price{
      color:$highlight-fontColor;
      font-size: .18rem;
    }
  }
  &_btn{
    width: .98rem;
    line-height: .5rem;
    background: #4fb0f9;
    color: $bg-color;
    font-size: .14rem;
    text-align: center;
    a{
      text-decoration: none;
      color: #fff;
    }
  }
}
</style>
